<template>
  <!--订购节点新增  -->
  <div>
    <FormPanel ref="formPanel" :list="initForm" :cols="1" :labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { parseTime } from '@/utils/index';
import { insertDealerApplyOrder, updataDealerApplyOrder } from '@/api/cms-api/origin/construct/dealer';
export default {
  mixins: [dictionary],
  props: {
    value: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      saveLoading: false,
      formData: {}
    };
  },
  mounted() {
    //如果传入参数不为空，则说明是修改进入，初始化对应数据
    if (this.value != null && (this.value.type == 'edit' || this.value.type == 'show')) {
      this.init();
    }
  },
  methods: {
    //修改时，初始化页面数据
    init() {
      //循环为表单赋值
      this.initForm.forEach(x => {
        if (x.fieldName == 'orderTime' || x.fieldName == 'arrivalTime' || x.fieldName == 'installTime') {
          x.initialValue = parseTime(new Date(this.value.rowData[x.fieldName]), '{y}-{m}-{d} {h}:{i}:{s}');
        } else {
          x.initialValue = this.value.rowData[x.fieldName];
        }
      });
    },
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: '订购节点类型',
          fieldName: 'onodeType',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2548'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'DATE',
          label: '订购时间',
          fieldName: 'orderTime',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'DATE',
          label: '到货时间',
          fieldName: 'arrivalTime',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'INPUT_NUMBER',
          label: '订购金额',
          fieldName: 'orderFee',
          initialValue: 0,
          placeholder: '请输入',
          precision: 2,
          max: 99999999.99,
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'DATE',
          label: '安装时间',
          fieldName: 'installTime',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        }
      ];
    },
    formChangeHandle(val) {
      let orderTime = val.orderTime; //订购时间
      let arrivalTime = val.arrivalTime; //到货时间
      let installTime = val.installTime; //安装时间

      if (orderTime > arrivalTime) {
        this.$notify.error({ title: '提示信息', message: '订购时间不可以大于到货时间!' });
      } else if (arrivalTime > installTime) {
        this.$notify.error({ title: '提示信息', message: '到货时间不可以大于安装时间!' });
      } else {
        this.formData = val;
        this.doSaveDialog();
      }
    },
    //保存
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    //执行保存
    async doSaveDialog() {
      this.saveLoading = true;
      let submitData = Object.assign({}, { applyId: this.value.applyId }, this.formData);
      let res;
      //如果传入参数不为空并且为修改
      if (this.value != null && this.value.type == 'edit') {
        res = await updataDealerApplyOrder(submitData, this.value.rowData['applyOnodeId']);
      } else {
        res = await insertDealerApplyOrder(submitData);
      }
      if (res.resultCode === 200) {
        this.$notify.success({ title: '提示信息', message: '保存成功!' });
        // 重新加载
        this.closeDialog('refresh');
      } else {
        this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
        this.closeDialog();
      }
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', { visible: false, type: type });
    }
  }
};
</script>
